package com.javajun.endofterm.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.javajun.endofterm.common.CommonEntity;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 宿舍实体类
 * Author: Xiong Limin
 * Date: 2020-11-30-8:37 下午
 */
@Entity
public class Domitory extends CommonEntity<Integer> {
    private String name; // 宿舍名字
    private Integer severalPeople; // 几个人
    private Double surplusPower; //剩余电量

    // 级联保存、更新、删除、刷新;延迟加载。当删除用户，会级联删除该用户的所有文章
    // 拥有mappedBy注解的实体类为关系被维护端
    @JsonIgnore
    @OneToMany(mappedBy = "domitory", fetch = FetchType.LAZY) // 默认也是 lazy，可以不用配置
    @NotFound(action= NotFoundAction.IGNORE)
    private List<Student> studentList = new ArrayList<>(); //学生

    @JsonIgnore
    @OneToMany(mappedBy = "domitory")
    private List<PowerRecord> powerRecordList = new ArrayList<>(); // 用电详情

    @JsonIgnore
    @OneToMany(mappedBy = "domitory")
    private List<ExpressDelivery> expressDeliveryList = new ArrayList<>(); //快递

    /**
     * 未取件快递数,不持久化
     * @return 未取件快递数量
     */
    @Transient
    public int getExpressDeliveryNumber() {
        int count = 0;
        if (expressDeliveryList != null) {
            for (ExpressDelivery e : expressDeliveryList) {
                if (e.getWhetherPickUp() == 0) {
                    count ++;
                }
            }
        }
        return count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    public Integer getSeveralPeople() {
        return severalPeople;
    }

    public void setSeveralPeople(Integer severalPeople) {
        this.severalPeople = severalPeople;
    }

    public List<PowerRecord> getPowerRecordList() {
        return powerRecordList;
    }

    public void setPowerRecordList(List<PowerRecord> powerRecordList) {
        this.powerRecordList = powerRecordList;
    }

    public List<ExpressDelivery> getExpressDeliveryList() {
        return expressDeliveryList;
    }

    public void setExpressDeliveryList(List<ExpressDelivery> expressDeliveryList) {
        this.expressDeliveryList = expressDeliveryList;
    }

    public Double getSurplusPower() {
        return surplusPower;
    }

    public void setSurplusPower(Double surplusPower) {
        this.surplusPower = surplusPower;
    }
}
